package com.cy.aircompressor.main.view.dialog;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.WindowManager;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;

import com.cy.aircompressor.R;
import com.cy.aircompressor.databinding.DialogPickerLayoutBinding;
import com.drug.lib.util.ToastUtil;
import com.github.gzuliyujiang.wheelpicker.contract.OnDatimeSelectedListener;
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity;
import com.github.gzuliyujiang.wheelpicker.entity.DatimeEntity;
import com.github.gzuliyujiang.wheelpicker.entity.TimeEntity;
import com.jakewharton.rxbinding2.view.RxView;
import com.net.model.base.fragment.BaseVBDialogFragment;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

public class DateTimePickerDialog extends BaseVBDialogFragment<DialogPickerLayoutBinding> {
    boolean isStart = true;

    public static DateTimePickerDialog showDialogFragment(FragmentManager fragmentManager, String tag, long minDate) {
        Bundle args = new Bundle();
        DateTimePickerDialog fragment = new DateTimePickerDialog();
        args.putLong("min_date", minDate);
        fragment.setArguments(args);
        fragment.show(fragmentManager, tag);
        return fragment;
    }

    @Override
    protected DialogPickerLayoutBinding getViewBinding() {
        return DialogPickerLayoutBinding.inflate(getLayoutInflater());
    }

    @Override
    protected int getWindowHeight() {
        return WindowManager.LayoutParams.WRAP_CONTENT;
    }

    @Override
    protected int getWindowWidth() {
        return getScreenWidth() / 2;
    }

    @Override
    public boolean isCancelable() {
        return false;
    }

    @Override
    protected void initialize() {

        long minDate = getArguments().getLong("min_date");
        if (minDate == 0) {
            Calendar minCalendar = new GregorianCalendar(2010, 1, 1);
            minDate = minCalendar.getTimeInMillis();
        }
//        mBinding.picker.setMaxMillisecond(System.currentTimeMillis());
//        mBinding.picker.setMinMillisecond(minDate);
//        mBinding.picker.setDefaultMillisecond(System.currentTimeMillis());
//        mBinding.picker.setOnDateTimeChangedListener(new DateTimePicker.OnDateTimeChangedListener() {
//            @Override
//            public void onDateTimeChanged(@Nullable DateTimePicker dateTimePicker, long l) {
//                if (isStart) {
//                    mBinding.startTimeText.setText(TimeUtils.date2String(new Date(l)));
//                } else {
//                    mBinding.endTimeText.setText(TimeUtils.date2String(new Date(l)));
//                }
//            }
//        });
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(minDate));
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        DatimeEntity startDatime = new DatimeEntity();
        startDatime.setDate(DateEntity.target(year, month, day));
        startDatime.setTime(TimeEntity.target(hour, minute, second));
        mBinding.datimeWheelLayout.setRange(startDatime, DatimeEntity.now(),startDatime);
        mBinding.datimeWheelLayout.setOnDatimeSelectedListener(new OnDatimeSelectedListener() {
            @Override
            public void onDatimeSelected(int year, int month, int day, int hour, int minute, int second) {
                String time = String.format("%d-%d-%d %d:%d:%d",year,month,day,hour,minute,second);
                if (isStart) {
                    mBinding.startTimeText.setText(time);
                } else {
                    mBinding.endTimeText.setText(time);
                }
            }
        });
        setSelectBg();
        RxView.clicks(mBinding.startTimeLayout)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    isStart = true;
                    setSelectBg();
                });
        RxView.clicks(mBinding.endTimeLayout)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    isStart = false;
                    setSelectBg();
                });

        RxView.clicks(mBinding.cancelButton)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    if (dateTimeSelectCallBack != null) dateTimeSelectCallBack.cancelSelect();
                    dismiss();
                });
        RxView.clicks(mBinding.verifyButton)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    String startTime = mBinding.startTimeText.getText().toString().trim();
                    String endTime = mBinding.endTimeText.getText().toString().trim();
                    if (TextUtils.isEmpty(startTime)) {
                        ToastUtil.show("请选择开始查询时间");
                        return;
                    }
                    if (TextUtils.isEmpty(endTime)) {
                        ToastUtil.show("请选择结束查询时间");
                        return;
                    }
                    if (dateTimeSelectCallBack != null)
                        dateTimeSelectCallBack.selectDate(startTime, endTime);
                    dismiss();
                });
        RxView.clicks(mBinding.currentTime)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
//                    mBinding.picker.setDefaultMillisecond(System.currentTimeMillis());
                });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void requestNetwork() {

    }

    private void setSelectBg() {
        if (isStart) {
            mBinding.startTimeLayout.setBackground(ContextCompat.getDrawable(mContext, R.drawable.btn_nav_bg_selector));
            mBinding.endTimeLayout.setBackground(ContextCompat.getDrawable(mContext, R.drawable.btn_nav_bg_unselector));
        } else {
            mBinding.startTimeLayout.setBackground(ContextCompat.getDrawable(mContext, R.drawable.btn_nav_bg_unselector));
            mBinding.endTimeLayout.setBackground(ContextCompat.getDrawable(mContext, R.drawable.btn_nav_bg_selector));
        }
    }

    DateTimeSelectCallBack dateTimeSelectCallBack;

    public void setDateTimeSelectCallBack(DateTimeSelectCallBack dateTimeSelectCallBack) {
        this.dateTimeSelectCallBack = dateTimeSelectCallBack;
    }

    public interface DateTimeSelectCallBack {
        void selectDate(String startTime, String endTime);

        void cancelSelect();
    }
}
